package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Medicine;
import com.woniuxy.oldmanapi.service.MedicineService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class MedicineController {
    @Autowired
    MedicineService medicineService;
    @Value("${uploadPath}")
    private String uploadUrlPath;

    @GetMapping("/medicine/search")
    public ResponseResult<PageInfo<Medicine>> searchMedicine(String drugname,
                                                             @RequestParam(value = "size", defaultValue = "3") int size,
                                                             @RequestParam(value = "now", defaultValue = "1") int now) {

        ResponseResult<PageInfo<Medicine>> result = new ResponseResult<>();
        try {
            PageInfo<Medicine> pageInfo = medicineService.findMedicineByCons(drugname, size, now);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }

    @PostMapping("/medicine/add")
    public ResponseResult<Void> addMedicine(@RequestBody Medicine medicine) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            medicineService.addMedicine(medicine);
            result.setCode(200);
            result.setMsg("添加成功");

        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return result;
    }

    @PostMapping("/medicine/modify")
    public ResponseResult<Void> modifyMedicine(@RequestBody Medicine medicine) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            medicineService.modifyMedicine(medicine);
            result.setCode(200);
            result.setMsg("修改成功");

        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }


    @DeleteMapping("/medicine/remove/{mid}")
    public ResponseResult<Void> removeMedicineById(@PathVariable("mid") int mid) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            medicineService.removeMedicineById(mid);
            result.setCode(200);
            result.setMsg("删除成功");

        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return result;
    }

    @PostMapping("/medicine/uploadImg")
    public ResponseResult<String> uploadImgsrc(@RequestParam("uploadImg") MultipartFile multipartFile) {
        ResponseResult<String> result = new ResponseResult<>();
        try {
            String filename = multipartFile.getOriginalFilename();
         filename= UUID.randomUUID().toString().replace("-", "") + filename.substring(filename.lastIndexOf("."));
            File file=new File(uploadUrlPath,filename);
            multipartFile.transferTo(file);
            result.setData(filename);
            result.setMsg("上传成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("上传失败");
        }
        return result;
    }


    @GetMapping("/medicine/selectAll")
    public ResponseResult<List<Medicine>> selectAll() {

        ResponseResult<List<Medicine>> result = new ResponseResult<>();
        try {
           List<Medicine> list = medicineService.findAllMedicine();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }
}
